package com.shimao.celnet.payment.service.impl;

import com.shimao.celnet.payment.model.PInvoiceRed;
import com.shimao.celnet.payment.model.PInvoiceRedDetail;
import com.shimao.celnet.payment.service.PInvoiceRedDetailService;
import com.shimao.celnet.payment.service.PInvoiceRedService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PInvoiceRedServiceImpl extends BaseServiceImpl<PInvoiceRed> implements PInvoiceRedService{

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required = false)
    private PInvoiceRedDetailService pInvoiceRedDetailService;
//    @Override
//    public PInvoiceRedForm getByPage(PInvoiceRedForm formBean, int pageNum, int length) {
//        logger.info("分页查询--开始");
//        //分页查询
//        Example example = new Example(PInvoiceRed.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (StringUtil.isNotEmpty(formBean.getBusinessType())) {
//            //业务类型
//            criteria.andLike("businessType", "%" + formBean.getBusinessType() + "%");
//        }
//
//        if (StringUtil.isNotEmpty(formBean.getInvoiceTypeCode())) {
//            //发票种类编号
//            criteria.andLike("invoiceTypeCode", "%" + formBean.getInvoiceTypeCode() + "%");
//        }
//
//        if (StringUtil.isNotEmpty(formBean.getSerialNo())) {
//            //开票流水号
//            criteria.andLike("serialNo", "%" + formBean.getSerialNo() + "%");
//        }
//
//        if (StringUtil.isNotEmpty(formBean.getSellerTaxNo())) {
//            //销方单位税号
//            criteria.andLike("sellerTaxNo", "%" + formBean.getSellerTaxNo() + "%");
//        }
//
//
//        //分页查询
//        PageHelper.startPage(pageNum, length);
//        List<PInvoiceRed> tableList = getByExample(example);
//        formBean.setPageInfo(tableList);
//        return  formBean;
//    }

    /**
     * 保存发票记录以及该记录的明细
     * create by stan.ruan on 2018-01-04
     */
    @Override
    public int save(PInvoiceRed sBean, List<PInvoiceRedDetail> pInvoiceRedDetailList) {
        int retCnt = 0;
        Date date = new Date();
        String loginUserGuid = FinalUtil.INTER_FACE_USER_ID;
        sBean.setCreatedUserGuid(loginUserGuid);
        sBean.setUpdateDate(date);
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedDate(date);
        //新增蓝票记录
        retCnt = super.save(sBean);
        //新增付款记录明细
        String invoiceGuid = sBean.getGuid();
        for (PInvoiceRedDetail pInvoiceRedDetail : pInvoiceRedDetailList) {
            pInvoiceRedDetail.setInvoiceGuid(invoiceGuid);//蓝票记录GUID
            pInvoiceRedDetail.setCreatedUserGuid(loginUserGuid);
            pInvoiceRedDetail.setUpdateDate(date);
            pInvoiceRedDetail.setUpdateUserGuid(loginUserGuid);
            pInvoiceRedDetail.setCreatedDate(date);
            //蓝票记录明细保存
            pInvoiceRedDetailService.save(pInvoiceRedDetail);
        }
        return retCnt;
    }



    /**
     * 通过开票guid，查询红票记录以及红票记录明细
     * create by stan.ruan on 2018-01-04
     */
    @Override
    public Map<String,Object> getInfo(String invoiceGuid) {
        Map<String,Object> retMap =new HashMap<>();
        //蓝票详情获取
        retMap.put("pInvoiceRed",this.getByKey(invoiceGuid));

        //蓝票详情的开票明细获取
        retMap.put("pInvoiceDetailList",pInvoiceRedDetailService.getByInvoiceGuid(invoiceGuid));

        return retMap;
    }


}
